package com.zw.a.basic.a10.demo01_collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

//案例: 演示Collection集合存储自定义对象, 并遍历.
//考察: 集合的使用步骤(4大步3小步)
public class Demo04 {
    public static void main(String[] args) {
        //1. 创建集合对象.
        Collection<Student> coll = new ArrayList<>();
        //2. 创建元素对象.
        Student s1 = new Student("刘亦菲", 33);
        Student s2 = new Student("赵丽颖", 31);
        Student s3 = new Student("高圆圆", 35);
        //Student s4 = new Student("丹丹", 18);
        //3. 把元素对象添加到集合对象中.
        coll.add(s1);
        coll.add(s2);
        coll.add(s3);
        //coll.add(s4);
        //4. 遍历集合.
        //4.1 根据集合对象获取其对应的迭代器对象.          Collection#iterator();
        Iterator<Student> it = coll.iterator();
        //4.2 判断迭代器中是否有下一个元素.                Iterator#hasNext();
        while (it.hasNext()) {
            //4.3 有就获取.                                Iterator#next();
            Student stu = it.next();
            System.out.println(stu);
            //不能写成如下的形式, 因为next()方法调用一次, 就会获取一个值.
            //下边这个代码就属于: 判断一次, 获取两个值.
            //System.out.println(it.next().getName() + "..." + it.next().getAge());
        }
    }
}
